package com.example.controller;

import com.example.common.Result;
import com.example.entity.Position;
import com.example.service.PositionService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 职位前端操作接口
 */
@RestController
@RequestMapping("/position")
public class PositionController {

    @Resource
    private PositionService positionService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Position position) {
        positionService.add(position);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        positionService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        positionService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Position position) {
        positionService.updateById(position);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Position position = positionService.selectById(id);
        return Result.success(position);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Position position) {
        List<Position> list = positionService.selectAll(position);
        return Result.success(list);
    }

    /**
     * 根据部门ID查询职位
     */
    @GetMapping("/selectByDepartmentId/{departmentId}")
    public Result selectByDepartmentId(@PathVariable Integer departmentId) {
        List<Position> list = positionService.selectByDepartmentId(departmentId);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Position position,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Position> page = positionService.selectPage(position, pageNum, pageSize);
        return Result.success(page);
    }
}